<script type="text/javascript">
  /*<![CDATA[*/
  var ____pages = JSON.parse('<?php echo addslashes(json_encode($app_data->pages)); ?>');
  var ____root = Utils.get_root(____pages);
  var ____sys = '<?php echo addslashes($app_data->sys->php_name); ?>';
  var ____file = '<?php echo addslashes($app_data->param_file); ?>';
  var ____login_required = '<?php echo addslashes($app_data->sys->login_required); ?>';
  var ____is_authorised = '<?php echo addslashes((isset($app_data->is_authorised)) ? $app_data->is_authorised : 0); ?>';
  var ____id = '<?php echo addslashes($app_data->param_id); ?>';
  var ____default_id = '<?php echo addslashes($app_data->__default_id); ?>';
  var ____login_id = '<?php echo $app_data->__login_id; ?>';
  var ____redmd = '<?php echo addslashes($app_data->sys->redmd); ?>';
  
  $(function(){
    Utils.clean_datepicker();
    $(document).tooltip();
    if (____login_required == 0 || ____is_authorised == 1) {
      ____render();
    }
  });

  function ____render() {
    ____menubar(____id, ____root);
    ____leftbar(____id, ____file);
    ____nav(____id, ____file);
  };

  function ____leftbar(id, file) {
    if (file == 'index') {
      $("#cs_leftbar_objs").hide();
      $("#cs_leftbar").removeClass('cs_otherlink');
    } else {
      $("#cs_leftbar_objs").show();
      $("#cs_leftbar").addClass('cs_otherlink');
    }
    var left = ____get_left(____root, id);
    if (left || file != 'index') {
      $("#cs_leftbar").parent().show();
      if (left) {
        $("#cs_leftbar").show();
        $("#cs_leftbar").empty();
        $("#cs_leftbar").append(
          '<h3>' + ((file == 'index') ? left.name : 'Other Menu') + '</h3>' + 
          '<ul class="modern-menu mm-vertical"></ul>'
        );
        ____leftbar_node(left, $('#cs_leftbar .modern-menu'), id, file);
        $('#cs_leftbar .modern-menu').modernMenu();
      } else {
        $("#cs_leftbar").hide();
      }
    } else {
      $("#cs_leftbar").parent().hide();
    }
  };

  function ____get_left(node, id) {
    for (var idx in node.__c) {
      var itm = node.__c[idx];
      if (node.id != 0 && itm.id == id) {
        return node;
      } else if (node.id == 0 && itm.id == id && itm.__c.length > 0) {
        return itm;
      } else if (node.id == 0 && itm.id == id && itm.__c.length == 0) {
        return null;
      }
      var rslt = ____get_left(itm, id);
      if (rslt != null) {
        return rslt;
      }
    }
    return null;
  };

  function ____leftbar_node(node, elm, id, file) {
    for (var idx in node.__c) {
      var itm = node.__c[idx];
      elm.append('<li id="left_' + itm.id + '" ' + ((itm.id == id && file == 'index') ? 'class="modern-menu-hi"' : '') + '><a href="#" onclick="return Url.redirect(' + ____redmd + ', \'' + ____sys + '.php?i=' + itm.id + '\');"><span>' + ((itm.id == id && file != 'index') ? '* ' : '') + itm.name + '</span></a></li>');
      if (itm.__c.length > 0) {
        $('#left_' + itm.id).append('<ul id="leftc_' + itm.id + '"></ul>');
        ____leftbar_node(itm, $('#leftc_' + itm.id), id, file);
      }
    }
  };
  
  function ____nav(id, file) {
    var navs = ____get_navs(____root, id, file, []);
    if (navs.length > 0 /* && navs[0].id != ____default_id */) {
      $(".cs_navbar").show();
      $(".cs_navbar").empty();
      $(".cs_navbar").append('<button id="nav_home" onclick="return Url.redirect(' + ____redmd + ', \'' + ____sys + '.php?i=' + ____default_id + '\');">&nbsp;</button>');
      $('#nav_home').button({
        text: false,
        icons: {
          primary: "ui-icon-home"
        }
      });
      $(".cs_navbar").append('<button id="navsp_first" style="margin: 0 4px;" disabled="disabled">&nbsp;</button>');
      $('#navsp_first').button({
        text: false,
        icons: {
          primary: "ui-icon-play"
        }
      });
      for (var idx in navs) {
        var itm = navs[idx];
        $(".cs_navbar").append(
          '<button id="nav_' + itm.id + '" class="nav" onclick="return Url.redirect(' + ____redmd + ', \'' + ____sys + '.php?i=' + itm.id + '\');">' +
          itm.name + 
          '</button>'
        );
        $('#nav_' + itm.id).button();
        if (idx < navs.length - 1 || file != 'index') {
          $(".cs_navbar").append('<button id="navsp_' + itm.id + '" style="margin: 0 4px;" disabled="disabled">&nbsp;</button>');
          $('#navsp_' + itm.id).button({
            text: false,
            icons: {
              primary: "ui-icon-play"
            }
          });
        }
      }
      if (file != 'index') {
        file = file.replace('index_', '')
        file = file.charAt(0).toUpperCase() + file.substr(1);
        $(".cs_navbar").append(
          '<button id="nav_last" class="nav">' +
          file +
          '</button>'
        );
        $('#nav_last').button();
      }
    } else {
      $(".cs_navbar").hide();
    }
  };

  function ____get_navs(node, id, file, arr) {
    for (var idx in node.__c) {
      var itm = node.__c[idx];
      if (itm.id == id) {
        arr.push(itm);
        return arr;
      }
      var rslt = ____get_navs(itm, id, file, arr);
      if (rslt != false) {
        arr.splice(0, 0, itm);
        return rslt;
      }
    }
    return false;
  };
  
  function ____update(parsed_url, rslt, elm) {
    if (rslt == '') {
      $('.cs_body').hide();
    } else {
      $('.cs_body').show();
    }
    var id = parsed_url.queryKey.i ? parsed_url.queryKey.i : ____default_id;
    var file = parsed_url.queryKey.__file ? parsed_url.queryKey.__file : 'index';
    elm.html(rslt);
    $(document).tooltip();
    $('.cs_hi').removeClass('cs_hi');
    ____mid = ____mmid(____root, id);
    $('.cs_menubar li[value="' + ____mid + '"]').addClass('cs_hi');
    ____leftbar(id, file);
    ____nav(id, file);
  };

  function ____mmid(node, id) {
    for (var idx in node.__c) {
      var itm = node.__c[idx];
      if (node.id == 0 && itm.id == id) {
        return itm.id;
      } else if (node.id != 0 && itm.id == id) {
        return true;
      }
      var rslt = ____mmid(itm, id);
      if (node.id == 0 && rslt == true) {
        return itm.id;
      } else if (node.id != 0 && rslt == true) {
        return true;
      }
    }
    return false;
  };
  
  <?php echo $app_data->contents[$app_data->query]->js; ?>
  /*]]>*/
</script>